/**
 * 实现喜羊羊
 * 创建者：Carburn Ashroom
 * 2024.1.1
 */

package Char;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;

public class Sheep extends Char implements SheepConfig {        // Sheep类是喜羊羊

    private final boolean[] direct = new boolean[4];
    private final boolean keyBoard;
    private Point mousePos;

    public Sheep(boolean key, Dimension screen) throws IOException {
        super(FILE_NAME, new Point(), WIDTH, INIT_RIGHT, SPEED, screen);
        keyBoard = key;
        clearDirect();
    }

    public boolean ctrlByKey() {
        return keyBoard;
    }

    public void keyPress(KeyEvent e) {      // 按键盘
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                direct[Direct.LEFT.ordinal()] = true;
                break;
            case KeyEvent.VK_RIGHT:
                direct[Direct.RIGHT.ordinal()] = true;
                break;
            case KeyEvent.VK_UP:
                direct[Direct.UP.ordinal()] = true;
                break;
            case KeyEvent.VK_DOWN:
                direct[Direct.DOWN.ordinal()] = true;
                break;
        }
    }

    public void keyRelease(KeyEvent e) {        // 松开按键
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                direct[Direct.LEFT.ordinal()] = false;
                break;
            case KeyEvent.VK_RIGHT:
                direct[Direct.RIGHT.ordinal()] = false;
                break;
            case KeyEvent.VK_UP:
                direct[Direct.UP.ordinal()] = false;
                break;
            case KeyEvent.VK_DOWN:
                direct[Direct.DOWN.ordinal()] = false;
                break;
        }
    }

    public void update() {      // 更新位置
        if (!keyBoard) {
            walkCenterTo(mousePos.x, mousePos.y);
            return;
        }
        Point nowPos = getPos();
        if (direct[Direct.UP.ordinal()]) {
            nowPos.y -= getSpeed();
        }
        if (direct[Direct.RIGHT.ordinal()]) {
            nowPos.x += getSpeed();
        }
        if (direct[Direct.DOWN.ordinal()]) {
            nowPos.y += getSpeed();
        }
        if (direct[Direct.LEFT.ordinal()]) {
            nowPos.x -= getSpeed();
        }
        move(nowPos);
    }

    public void mouseMove(MouseEvent e) {
        mousePos = e.getPoint();
    }

    private void clearDirect() {        // 清除方向记录
        for (int i=0; i!=4; ++i) {
            direct[i] = false;
        }
        mousePos = new Point();
    }

}
